package main

import (
	"context"
	"fmt"
	"time"
)


func main() {
	ctx, cancel := context.WithCancel(context.Background())


	ch := make(chan int)
	go func() {
		for i := 0; ; i++ {
			time.Sleep(1*time.Second)
			select {
				case <- ctx.Done():
					fmt.Println(ctx.Err())
					return
				case ch <- i:
			}
		}
	} ()



	for v := range ch {
		fmt.Println(v)
		if v == 5 {
			cancel()
			break
		}
	}
	time.Sleep(1*time.Second)
}